﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.BillingServiceRef;
    using WMS6.Web.UserControls;

    public class InvoiceRptList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15621;
        protected GridView gvInvoice;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblAllTotalAmount;
        protected Label lblAllTotalAmountText;
        protected Label lblInvoiceList;
        protected Label lblMsg;
        protected LinkButton lnkbtnAmountCancle;
        protected LinkButton lnkbtnAmountSubmit;
        protected LinkButton lnkbtnInvoiceCancle;
        protected LinkButton lnkbtnInvoiceSubmit;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnAmountCancle;
        protected HtmlTableCell tdlnkbtnAmountSubmit;
        protected HtmlTableCell tdlnkbtnInvoiceCancle;
        protected HtmlTableCell tdlnkbtnInvoiceSubmit;
        private decimal totalAmount;
        protected UpdatePanel up1;

        private void BindData2InvoiceList(InvoiceRptQueryEntity invoiceRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(invoiceRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetInvoiceRptDataSet(invoiceRptQueryEntity);
            this.gvInvoice.Visible = true;
            base.BindData2GridView(this.gvInvoice, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = invoiceRptQueryEntity.CurrentPage;
            invoiceRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetInvoiceRptDataSet(invoiceRptQueryEntity))
                {
                    base.Export2Excel("InvoiceRpt.xls", "15621", this.gvInvoice, ds);
                }
            }
            finally
            {
                invoiceRptQueryEntity.IsGetAll = false;
                invoiceRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetInvoiceQueryEntity(invoiceRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.CurrentPage = "0";
            this.BindData2InvoiceList(invoiceRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2InvoiceList(invoiceRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.INVOICE_ID = "";
            invoiceRptQueryEntity.OWNER_ID = "";
            invoiceRptQueryEntity.INVOICE_STATUS = "";
            invoiceRptQueryEntity.STATEMENT_ID = "";
            invoiceRptQueryEntity.INVOICE_SUBMIT = "";
            invoiceRptQueryEntity.PAY_SUBMIT = "";
            invoiceRptQueryEntity.StartDateTime = "";
            invoiceRptQueryEntity.EndDateTime = "";
            this.BindData2InvoiceList(invoiceRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            if (int.Parse(invoiceRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                invoiceRptQueryEntity.CurrentPage = (int.Parse(invoiceRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2InvoiceList(invoiceRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            if (int.Parse(invoiceRptQueryEntity.CurrentPage) > 0)
            {
                invoiceRptQueryEntity.CurrentPage = (int.Parse(invoiceRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2InvoiceList(invoiceRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            this.BindData2InvoiceList(invoiceRptQueryEntity);
        }

        private InvoiceRptQueryEntity GetInvoiceQueryEntity()
        {
            return (base.PageQueryEntitySession as InvoiceRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
                invoiceRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2InvoiceList(invoiceRptQueryEntity);
            }
        }

        protected void gvInvoice_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15621", this.gvInvoice);
        }

        protected void gvInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblCompany"));
                }
                if ((((Label) e.Row.FindControl("lblInvoiceAmount")) != null) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblInvoiceAmount")).Text))
                {
                    this.totalAmount += decimal.Parse(((Label) e.Row.FindControl("lblInvoiceAmount")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblAllTotalAmountText.Text = double.Parse(this.totalAmount.ToString()).ToString();
            }
        }

        protected void gvInvoice_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
                invoiceRptQueryEntity.SortField = e.SortExpression;
                if (invoiceRptQueryEntity.SortDirection == "ASC")
                {
                    invoiceRptQueryEntity.SortDirection = "DESC";
                }
                else if (invoiceRptQueryEntity.SortDirection == "DESC")
                {
                    invoiceRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2InvoiceList(invoiceRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnAmountCancle_Click(object sender, EventArgs e)
        {
            this.UpdateInvoiceSubmit("AMOUNT_CANCLE");
        }

        protected void lnkbtnAmountSubmit_Click(object sender, EventArgs e)
        {
            this.UpdateInvoiceSubmit("AMOUNT_SUBMIT");
        }

        protected void lnkbtnInvoiceCancle_Click(object sender, EventArgs e)
        {
            this.UpdateInvoiceSubmit("INVOICE_CANCLE");
        }

        protected void lnkbtnInvoiceSubmit_Click(object sender, EventArgs e)
        {
            this.UpdateInvoiceSubmit("INVOICE_SUBMIT");
        }

        private void LoadData()
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = new InvoiceRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    invoiceRptQueryEntity.INVOICE_ID = this.PreviousPage.InvoiceID;
                    invoiceRptQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    invoiceRptQueryEntity.INVOICE_STATUS = this.PreviousPage.InvoiceStatus;
                    invoiceRptQueryEntity.STATEMENT_ID = this.PreviousPage.StatementID;
                    invoiceRptQueryEntity.INVOICE_SUBMIT = this.PreviousPage.InvoiceSubmit;
                    invoiceRptQueryEntity.PAY_SUBMIT = this.PreviousPage.AmountSubmit;
                    invoiceRptQueryEntity.StartDateTime = this.PreviousPage.StartDate;
                    invoiceRptQueryEntity.EndDateTime = this.PreviousPage.EndDate;
                }
                else
                {
                    invoiceRptQueryEntity.INVOICE_ID = "";
                    invoiceRptQueryEntity.OWNER_ID = "";
                    invoiceRptQueryEntity.INVOICE_STATUS = "";
                    invoiceRptQueryEntity.STATEMENT_ID = "";
                    invoiceRptQueryEntity.INVOICE_SUBMIT = "";
                    invoiceRptQueryEntity.PAY_SUBMIT = "";
                    invoiceRptQueryEntity.StartDateTime = "";
                    invoiceRptQueryEntity.EndDateTime = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            invoiceRptQueryEntity.IsGetAll = false;
            invoiceRptQueryEntity.CurrentPage = "0";
            invoiceRptQueryEntity.PageSize = this.gvInvoice.PageSize.ToString();
            invoiceRptQueryEntity.SortField = " OWNER_ID";
            invoiceRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvInvoice.PageSize;
            this.SetInvoiceQueryEntity(invoiceRptQueryEntity);
            this.BindData2InvoiceList(invoiceRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvInvoice);
                this.LoadData();
            }
        }

        private void SetInvoiceQueryEntity(InvoiceRptQueryEntity invoiceRptQueryEntity)
        {
            base.PageQueryEntitySession = invoiceRptQueryEntity;
        }

        private void UpdateInvoiceSubmit(string action)
        {
            string check = string.Empty;
            try
            {
                List<InvoiceInfo> invoiceListInfo = new List<InvoiceInfo>();
                for (int i = 0; i < this.gvInvoice.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvInvoice.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        string invoiceSubmit = this.gvInvoice.DataKeys[i]["IS_INVOICE_SUBMIT"].ToString();
                        string amountSubmit = this.gvInvoice.DataKeys[i]["IS_AMOUNT_SUBMIT"].ToString();
                        if (action == "INVOICE_SUBMIT")
                        {
                            invoiceSubmit = "Y";
                        }
                        else if (action == "AMOUNT_SUBMIT")
                        {
                            invoiceSubmit = "Y";
                            amountSubmit = "Y";
                        }
                        else if (action == "INVOICE_CANCLE")
                        {
                            invoiceSubmit = "N";
                            if (amountSubmit == "Y")
                            {
                                check = check + this.gvInvoice.DataKeys[i]["INVOICE_ID"].ToString() + ",";
                            }
                        }
                        else if (action == "AMOUNT_CANCLE")
                        {
                            amountSubmit = "N";
                        }
                        InvoiceInfo InvoiceInfo = new InvoiceInfo {
                            InvoiceID = this.gvInvoice.DataKeys[i]["INVOICE_ID"].ToString(),
                            UpdatedBy = base.UserInfo.UserID,
                            IsInvoiceSubmit = invoiceSubmit,
                            IsAmountSubmit = amountSubmit
                        };
                        invoiceListInfo.Add(InvoiceInfo);
                    }
                }
                if (!string.IsNullOrEmpty(check))
                {
                    this.lblMsg.Text = "操作失败!发票号：" + check.Substring(0, check.Length - 1) + "已款项上交，不能发票取消";
                }
                else
                {
                    if (invoiceListInfo.Count > 0)
                    {
                        if (string.IsNullOrEmpty(BillingServiceClientProxy.GetInstance().UpdateInvoiceSubmit(invoiceListInfo)))
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F9210_025");
                        }
                        else
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F9210_024");
                        }
                    }
                    InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
                    this.BindData2InvoiceList(invoiceRptQueryEntity);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9210_025") + ex.Message;
            }
        }

        public InvoiceRptQuery PreviousPage
        {
            get
            {
                return (InvoiceRptQuery) base.PreviousPage;
            }
        }
    }
}

